package com.jeff.controller;

import com.jeff.common.BaseInfo;
import com.jeff.service.AdminService;
import com.jeff.service.DeptService;
import com.jeff.util.HTMLRequest;
import com.jeff.util.ResultMap;
import com.jeff.vo.Admin;
import com.jeff.vo.AdminLoginVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("admin")
public class AdminController {

    @Autowired
    public AdminService adminService;
    @Autowired
    public DeptService deptService;

    @RequestMapping("login")
    @ResponseBody
    public ResultMap login(AdminLoginVo adminLoginVo, HttpServletRequest request) {
        ResultMap rm = new ResultMap();
        try {
            // 判断验证码
            if (HTMLRequest.GetParameter(request, "yzm").equals(
                    BaseInfo.Getyzm(request))||true) {
                Admin admin = adminService.login(adminLoginVo);
                if (admin != null) {
                    BaseInfo.SetUserName(request, admin.getLoginName());
                    BaseInfo.SetName(request, admin.getName());
                    BaseInfo.SetUserGradeId(request, admin.getGradeId());
                    BaseInfo.SetUserDeptId(request, admin.getDeptId());
                    BaseInfo.SetUserDeptName(request, admin.getDeptName());
                    BaseInfo.SetUserRootDeptId(request,deptService.getRootDeptId(admin.getDeptId()));
                    BaseInfo.setUserId(request, admin.getId());
                    rm.data(admin);
                    rm.success().info("success");
                } else {
                    rm.fail().msg("账号密码不匹配");
                }
            } else {
                rm.fail().msg("验证码错误");
            }

        } catch (Exception e) {
            e.printStackTrace();
            rm.fail().info("fail");
        }
        return rm;
    }


    @RequestMapping("logout")
    @ResponseBody
    public ResultMap logout(HttpServletRequest request) {
        ResultMap rm = new ResultMap();
        try {
            request.getSession().invalidate();
            rm.success().info("success");
        } catch (Exception e) {
            e.printStackTrace();
            rm.fail().info("fail");
        }
        return rm;
    }

    @RequestMapping("checklogin")
    @ResponseBody
    public ResultMap checkLogin(HttpServletRequest request) {
        ResultMap rm = new ResultMap();
        Admin admin = new Admin();
        try {
            if (!"".equals(BaseInfo.GetUserName(request))) {
                admin.setLoginName(BaseInfo.GetUserName(request));
                admin.setName(BaseInfo.GetName(request));
                admin.setGradeId(BaseInfo.GetUserGradeId(request));
                rm.success().info("已登录").data(admin);
            } else {
                rm.fail().info("未登录");
            }
        } catch (Exception e) {
            e.printStackTrace();
            rm.fail().info("未登录");
        }
        return rm;
    }


}
